* Load the data file -----------------------------------------------------------

	if "$individual_run" != "Yes" {
	di "Program Stopped: Make sure you load the parameters in master.do"
	e
	}

	use $interim_data, clear

* Unit switched: Calculation ---------------------------------------------------

preserve

	* Sample restriction
	keep if inrange(month, 1, 12)
	
	* Definition Unit switch for any individual month
	gen switch_month_u  = (u~=unit_rand) ///
			if !missing(unit_rand) & !missing(u)
	gen switch_month_uw = (uw~=unit_watch_rand) ///
			if !missing(unit_watch_rand) & !missing(uw)
	
	* Collapse 
	collapse (mean) switch_month_u switch_month_uw, ///
	by(employee_id treatment strata unit_rand unit_watch_rand ///
	$consideration)
	
	* Definition on officer level
	
		* Ever switch
			gen switch_ever_uw = (switch_month_uw > 0 & !missing(switch_month_uw)) ///
			if !missing(unit_watch_rand)
			gen switch_ever_u  = (switch_month_u  > 0 & !missing(switch_month_u) ) ///
			if !missing(unit_rand)

		* Switch all
			gen switch_all_uw 	= (switch_month_uw == 1) ///
			if !missing(unit_watch_rand)
			gen switch_all_u 	= (switch_month_u == 1)					///
			if !missing(unit_rand)
			
		* Switch for more than half of the time
			gen switch_major_uw = inrange(switch_month_uw, 0.50001, 1)	///
			if !missing(unit_watch_rand)
			gen switch_major_u 	= inrange(switch_month_u, 0.50001, 1)	///
			if !missing(unit_rand)
	
	* Regression OLS
	reg_unit_switch OLS switch_ever_uw "" 	def1_uw
	reg_unit_switch OLS switch_ever_u "" 	def1_u
	reg_unit_switch OLS switch_all_uw "" 	def2_uw
	reg_unit_switch OLS switch_all_u "" 	def2_u	
	reg_unit_switch OLS switch_major_uw "" 	def3_uw
	reg_unit_switch OLS switch_major_u "" 	def3_u
		
restore
	

* Unit Swtich: Write to table --------------------------------------------------

	* Table B4	
	table_start_horizontal_us ${report_folder}/Admin_App_unit_switch.txt ///
	"Unit Switching" 200 4 "Control Mean" Sit-D SE N 	c p{1.4cm} p{1.4cm} p{1cm}
	midrule
	
		file write table "\\"
		file write table "\multicolumn{5}{c}{\textbf{Panel A: Unit $\times$ Watch Switch}}  \\"
		midrule
		write_horizontal_unit_switch def1_uw "Ever switched in any post-period month" 					CmCoSeN
		write_horizontal_unit_switch def3_uw "Switched for more than half the months in post-period" 	CmCoSeN
		write_horizontal_unit_switch def2_uw "Switched for all months in post-period" 					CmCoSeN
		
		file write table "\\"
		file write table "\multicolumn{5}{c}{\textbf{Panel B: Unit Switch}}  \\"
		midrule
			
		write_horizontal_unit_switch def1_u "Ever switched in any post-period month" 					CmCoSeN
		write_horizontal_unit_switch def3_u "Switched for more than half the months in post-period" 	CmCoSeN
		write_horizontal_unit_switch def2_u "Switched for all months in post-period" 					CmCoSeN
		
	table_end "$admin_unit_switch" Admin_unit_switch 
	
		